package com.play.common.result;

import cn.hutool.json.JSONArray;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回对象
 */
@ApiModel(value = "CommonResult",description = "接口统一返回对象")
public class CommonResult<T> {
    @ApiModelProperty("返回码(200表示成功，其他表示失败)")
    private long code;

    @ApiModelProperty("提示信息")
    private String message;

    @ApiModelProperty("数据")
    private T data;

    protected CommonResult() {
    }

    protected CommonResult(long code, String message, T data) {
        this.code = code;
        this.message = message+"！";
        if(data==null)
        {
            data= (T) new HashMap();//data为null时返回的json中显示为{}
        }

        this.data = data;
    }

    /**
     * 返回分页列表json
     * @param code 请求响应码
     * @param message 提示消息
     * @param total 数据总条数
     * @param pageIndex 当前页码
     * @param pageSize 页大小
     * @param list 分页列表
     */
    protected CommonResult(long code, String message,Long total,Long pageIndex,int pageSize,T list) {
        this.code = code;
        this.message = message+"！";
        if(list==null)
        {
            list= (T) new HashMap();//data为null时返回的json中显示为{}
        }

        Map<String,Object> result=new HashMap<>();
        result.put("list",list);
        result.put("total",total);
        result.put("pageIndex",pageIndex);
        result.put("pageSize",pageSize);

        this.data = (T) result;
    }

    /**
     * 成功返回分页列表
     */
    public static <T> CommonResult<T> success(Long total,Long pageIndex,int pageSize,T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),total,pageIndex,pageSize, data);
    }

    /**
     * 返回分页列表json
     * @param code 请求响应码
     * @param message 提示消息
     * @param total 数据总条数
     * @param pageIndex 当前页码
     * @param pageSize 页大小
     * @param list 分页列表
     */
    protected CommonResult(long code, String message,Long total,Long pageIndex,int pageSize
            ,T list,Map<String,Object> otherInfo) {
        this.code = code;
        this.message = message+"！";
        if(list==null)
        {
            list= (T) new HashMap();//data为null时返回的json中显示为{}
        }
        if(otherInfo==null)
        {
            otherInfo=new HashMap();
        }
        Map<String,Object> result=new HashMap<>();
        result.put("list",list);
        result.put("total",total);
        result.put("pageIndex",pageIndex);
        result.put("pageSize",pageSize);
        result.put("otherInfo",otherInfo);

        this.data = (T) result;
    }

    /**
     * 返回分页列表json
     * @param code 请求响应码
     * @param message 提示消息
     * @param list 分页列表
     */
    protected CommonResult(long code, String message,T list,Map<String,Object> otherInfo) {
        this.code = code;
        this.message = message+"！";
        if(list==null)
        {
            list= (T) new HashMap();//data为null时返回的json中显示为{}
        }
        if(otherInfo==null)
        {
            otherInfo=new HashMap();
        }
        Map<String,Object> result=new HashMap<>();
        result.put("list",list);
        result.put("otherInfo",otherInfo);
        this.data = (T) result;
    }

    /**
     * 成功返回分页列表
     */
    public static <T> CommonResult<T> success(Long total,Long pageIndex,int pageSize,T data,Map<String,Object> otherInfo) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),total,pageIndex,pageSize, data,otherInfo);
    }

    /**
     * 成功返回data和map
     */
    public static <T> CommonResult<T> success(T data,Map<String,Object> otherInfo) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data,otherInfo);
    }

    /**
     * 返回结果
     */
    public static CommonResult result(boolean isSuccess) {
        return isSuccess?success():failed();
    }

    /**
     * 成功返回结果
     */
    public static CommonResult success() {
        return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), new HashMap<>());
    }

    /**
     * 成功返回结果
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果(判断用户是否有权限)
     */
    public static CommonResult<Map<String,Object>> successHasPermit(boolean hasPermit) {
        Map<String,Object> map=new HashMap<>();
        map.put("hasPermit",hasPermit);
        return new CommonResult<Map<String,Object>>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), map);
    }

    /**
     * 成功返回结果
     */
    public static <T> CommonResult<T> success(T data, String message) {
        System.out.println(message);
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 成功返回结果
     */
    public static <T> CommonResult<T> success( String message) {
        return success(null,message);
    }


    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode) {
        System.out.println(errorCode.getMessage());
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(),null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode, String message) {
        System.out.println(message);
        return new CommonResult<T>(errorCode.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed(String message,T data) {
        System.out.println(message);
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, data);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果，data为空数组[]
     */
    public static <T> CommonResult<T> failedArray(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, (T) new JSONArray());
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
